package cn.itcast.bos.service.base.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.base.CouriterRepository;
import cn.itcast.bos.dao.base.FixedAreaRepository;
import cn.itcast.bos.dao.base.TakeTimeRepository;
import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.domain.base.TakeTime;
import cn.itcast.bos.service.base.FixedAreaSerivce;
@Service
@Transactional
public class FixedAreaServiceimpl implements FixedAreaSerivce{
    @Autowired
	private FixedAreaRepository fixedAreaRepository;
	
	@Override
	public void save(FixedArea fixedArea) {	 		  
           fixedAreaRepository.save(fixedArea);           
	}

	@Override
	public Page<FixedArea> findpageDate(Specification<FixedArea> specification, Pageable pageable) {
		
		return fixedAreaRepository.findAll(specification,pageable);
		
	}
   
	//注入
	@Autowired
	private TakeTimeRepository takeTimeRepository;
	@Autowired
	private  CouriterRepository couriterRepository;
	
	@Override
	public void associationCourierToFixedArea(FixedArea fixedArea, Integer courierId, Integer takeTimeId) {
		//先根据id查询到三个对象 在通过session(EntityManager)的自动更新 即可
		FixedArea fixed = fixedAreaRepository.findOne(fixedArea.getId());
		//findone方法 通过id查询对应的实体类 要通过该id对应的实体类dao层方法才可以 
		//这里查询courier  taketime所以需要注入属性
		Courier courier = couriterRepository.findOne(courierId);
		TakeTime takeTime = takeTimeRepository.findOne(takeTimeId);
		//定区关联快递员     因为定区是外键维护方
		fixed.getCouriers().add(courier);
		//快递员关联收派时间
		courier.setTakeTime(takeTime);
	}

}
